import java.util.concurrent.CountDownLatch;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-08-20
 * Time: 10:55
 * To BE A Better Person-2020-3-2
 */

public class TestDemo {
    public static void main(String[] args) throws InterruptedException {
        //构造 CountDownLatch 实例, 初始化 10 表示有 10 个任务需要完成.
        //每个任务执行完毕, 都调用 latch.countDown() . 在 CountDownLatch 内部的计数器同时自减.
        //主线程中使用 latch.await(); 阻塞等待所有任务执行完毕. 相当于计数器为 0 了.
        CountDownLatch latch=new CountDownLatch(5);
        Runnable run=new Runnable() {
            @Override
            public void run() {
                System.out.println("比赛");
                latch.countDown();
            }
        };
        for (int i = 0; i < 5; i++) {
            Thread t=new Thread(run);
            t.start();
        }

        latch.await();
        System.out.println("比赛结束；");
    }
}
